﻿using SqlScriptVault.Commands;
using SqlScriptVault.Model;
using SqlScriptVault.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace SqlScriptVault.ViewModel
{
    class ConnectionManagerViewModel : BaseNotifyViewModel
    {
        private readonly IFileService _fileService;
        private readonly IMessageDisplayService _messageDisplayService;

        private ServerGroup _currentServerGroup;

        private const string ConnectionVaultFileName = "ConnectionVault.dat";

        public ConnectionManagerViewModel()
            : this(new FileService(), new MessageDisplayService())
        {
        }

        public ConnectionManagerViewModel(IFileService fileService, IMessageDisplayService messageDisplayService)
        {
            _fileService = fileService;
            _messageDisplayService = messageDisplayService;

            if (_fileService.Exists(ConnectionVaultFileName))
            {
                ConnectionVault = _fileService.LoadFile<ConnectionVault>(ConnectionVaultFileName);
            }
            else
            {
                ConnectionVault = new ConnectionVault();

                _fileService.SaveFile<ConnectionVault>(ConnectionVaultFileName, ConnectionVault);
            }            
        }

        public RelayCommand NewServerGroupCommand
        {
            get
            {
                return new RelayCommand(param => NewServerGroup());
            }
        }

        public RelayCommand AddServerToGroupCommand
        {
            get
            {
                return new RelayCommand(param => AddServerToGroup());
            }
        }

        public ConnectionVault ConnectionVault { get; private set; }
        public ServerGroup CurrentServerGroup
        {
            get { return _currentServerGroup; }
            set
            {
                _currentServerGroup = value;

                RaisePropertyChanged("CurrentServerGroup");
            }
        }

        private void NewServerGroup()
        {
            ServerGroup serverGroup = _messageDisplayService.OpenServerGroup();
            ConnectionVault.Add(serverGroup);

            RaisePropertyChanged("ConnectionVault");

            _fileService.SaveFile<ConnectionVault>(ConnectionVaultFileName, ConnectionVault);
        }

        private void AddServerToGroup()
        {
            if (CurrentServerGroup == null)
            {
                _messageDisplayService.ShowErrorMessage("Choose a server group first.");
                return;
            }

            Server server = _messageDisplayService.OpenServer();

            if (server == null)
            {
                return;
            }

            CurrentServerGroup.Add(server);

            RaisePropertyChanged("CurrentServerGroup");

            _fileService.SaveFile<ConnectionVault>(ConnectionVaultFileName, ConnectionVault);
        }
    }
}
